package th.ac.mahidol.ict.is.mpay.seller;



import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
	private static final String TAG="Main";
	private Button submit;
	private Button config;
	//private EditText display;
	private String host;
	private String path="seller/";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        submit = (Button) findViewById(R.id.submit);
        config = (Button) findViewById(R.id.config);
        //display= (EditText) findViewById(R.id.resultEdt);
        submit.setOnClickListener(this);
        config.setOnClickListener(this);
        getDateStr();
        //display.setText(getDateStr());
        //display.setEnabled(true);
        jsonTest();
    }
	public void onClick(View sender) {
		if(sender == submit) {
			confirmPayment();
		}else if(sender == config) {
			showConfigAlert();
		}
		
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	private void confirmPayment() {
		try {
		FileGetContent.getContents(host+path+getConfirmString(), "UTF-8");
		} catch (Exception e) {
			Log.d(TAG,"can not connect to host");
		}
		
	}
	private String getConfirmString() {
		return null;
	}
	/**
	 * 
	 */
	private void showConfigAlert() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Config");
		alert.setMessage("Enter host's base url.");

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		input.setHint("localhost/");
		input.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString();
				host = value;
				Toast.makeText(Main.this, value, Toast.LENGTH_SHORT).show();
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						;
					}
				});
		alert.show();
	}
	
	private String getDateStr() {
		Date today = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(today);
	}
	
	private void jsonTest() {
		String json = "{\"menu\": {"+
		"\"id\": \"file\","+
		  "\"value\": \"File\"," +
		  "\"popup\": {" +
		  "\"menuitem\": [" +
		  "      {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}," +
		  "      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}," +
		  "      {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}" +
		  "    ]" +
		  "  }" +
		  "}}\";";
		try {
			JSONObject jsonO = new JSONObject(json);
			JSONObject menuObject = jsonO.getJSONObject("menu");
			String attId = menuObject.getString("id");
			String attValue = menuObject.getString("value");
			JSONObject attPopup = menuObject.getJSONObject("popup");
			JSONArray menuItemArray = attPopup.getJSONArray("menuitem");
			int lenght = menuItemArray.length();
			
			Log.d(TAG, "id:"+attId+" attValue:"+attValue);
			for(int i = 0;i< lenght; i++) {
				Log.d(TAG,"menu item, value:"+menuItemArray.getJSONObject(i).getString("value"));
				Log.d(TAG,"menu item, onClick:"+menuItemArray.getJSONObject(i).getString("onclick"));
				
			}
		} catch (JSONException e) {
			Log.d(TAG,"Json parsing error");
			e.printStackTrace();
		}
	}
}